/*
 * Copyright (C) 2009 Christoph Caks
 *
 * This file is part of GLWAK.
 *
 * GLWAK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GLWAK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GLWAK.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * glwakTest.cpp
 *
 *  Created on: 05.07.2009
 *      Author: Christoph Caks
 */

#include <iostream>

#ifdef GLWAK_WINDOWS
#include <windows.h>
#define sleep(n) Sleep(1000 * n)
#endif

#include "Context.h"
#include "Window.h"

#include "ContextConfiguration.h"
#include "WindowConfiguration.h"

#include "GLWAKFactory.h"

#include "OpenGL.h"

using namespace glwak;
using namespace std;

glwak::Window *window;
glwak::Context *context;

/*
 * TODO: on window creation check if fullscreen and init as fullscreen
 * TODO: find a way to update the size of the opengl render area
 *
 */
int windowTest() {

	try {

		WindowConfiguration windowConfig;
		windowConfig.setHeight( 768 );
		windowConfig.setWidth( 1280 );
		windowConfig.setFullscreen( true );
		WindowConfiguration windowConfig2(
				"glwakTest",
				800,
				600,
				false );
		ContextConfiguration contextConfig( 3, 0 );

		window = GLWAKFactory::createWindow();
		context = GLWAKFactory::createContext( window );

		window->setConfiguration( windowConfig2 );

		context->setConfiguration( contextConfig );

		//window->initialize();
		window->startMessageThread();

		while( !window->isInitialized() ) {
			sleep( 1 );
		}

		context->initialize();



		window->show();
		context->makeCurrent();

		//glViewport( 0, 0, 400, 400 );

		int  count = 0;
		bool alive = true;
		while ( alive ) {
			for ( int i = 0; i < 2; i++ ) {
				glClearColor(0, 0.5, 1, 1);
				glClear( GL_COLOR_BUFFER_BIT );
				context->swapBuffers();

				usleep( 1000000 );

				glClearColor(1, 0.5, 0, 1);
				glClear( GL_COLOR_BUFFER_BIT );
				context->swapBuffers();

				usleep( 1000000 );
			}
			if ( count % 2 == 0 ) {
				window->setConfiguration( windowConfig );
				//glViewport( 0, 0, windowConfig.getWidth(), windowConfig.getHeight() );
			}
			else {
				window->setConfiguration( windowConfig2 );
				//glViewport( 0, 0, windowConfig2.getWidth(), windowConfig2.getHeight() );
			}
			count ++;
		}
		window->hide();

		sleep( 1 );

		window->show();
		context->makeCurrent();
		//context->makeCurrent();
		glClearColor(1, 0.5, 0, 1);
		glClear( GL_COLOR_BUFFER_BIT );
		context->swapBuffers();
		sleep( 1 );

		glClearColor(0, 0.5, 1, 1);
		glClear( GL_COLOR_BUFFER_BIT );
		context->swapBuffers();
		sleep( 1 );

		window->hide();

		//context->dispose();

		//window->dispose();

		return 0;
	}
	catch ( char const *e ) {
		cout << "Exception has been thrown: " << e << endl;
		return 1;
	}

}

int main( int args, char** argv ) {
	return windowTest();
}
